package com.cya.forumproject.services.impl;

import com.cya.forumproject.common.AppResult;
import com.cya.forumproject.common.ResultCode;
import com.cya.forumproject.dao.BoardMapper;
import com.cya.forumproject.exception.ApplicationException;
import com.cya.forumproject.model.Board;
import com.cya.forumproject.services.IBoradService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/9/21 17:56
 * @description：
 * @modified By：
 * @version:
 */
@Slf4j
@Service
public class BoradServiceImpl implements IBoradService {

    @Resource
    private BoardMapper boardMapper;
    @Override
    public List<Board> selectByNum(Integer num) {
//         非空校验
        if (num<=0){
            //打印日志
      log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
//      抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Board> result = boardMapper.selectByNum(num);

        //返回结果
        return result;
    }

    @Override
    public Board selectById(Long id) {

        if (id==null||id<=0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        return board;
    }

    @Override
    public void addOneArticleCountById(Long id) {


        if (id==null||id<=0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board==null){
            //打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString()+",board id="+id);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));

        }
        //更新数量
        Board updateBoard=new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()+1);
        updateBoard.setUpdateTime(new Date());
        boardMapper.updateByPrimaryKeySelective(updateBoard);

    }

    @Override
    public void subOneArticleCountId(Long id) {

        if (id==null||id<=0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board==null){
            //打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString()+",board id="+id);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));

        }
        //更新数量
        Board updateBoard=new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()-1);
        if (updateBoard.getArticleCount()<0){
            updateBoard.setArticleCount(0);
        }
        updateBoard.setUpdateTime(new Date());
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (row!=1){
            log.warn(ResultCode.FAILED.toString()+", 受影响行数不等于1.");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}